﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;

namespace EasyCheque.CommonPages
{
    public partial class ChequeResetPassword : System.Web.UI.Page
    {
        public Stream CQFileStream;
//         Logic.Cheque cf;
        protected void Page_Load(object sender, EventArgs e)
        {
            bool needReturn = (Session["chequeIDToResetPassword"] == null);
            Table_Return.Visible = needReturn;
            Table_ResetPassowrdCheque.Visible = !needReturn;
        }

        protected void Button_ResetPassword_Click(object sender, EventArgs e)
        {
            // 在这里加入支票的重设密码代码。
            // 新密码：TextBox_NewPassword。
            Label_CommitMessage.Visible = true;
            if (Session["chequeIDToResetPassword"] != null)
            {
                if (Session["uid"] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                string CmdStr = @"SELECT [PASSWORD] FROM PERSON_INFO WHERE ID=" + Session["uid"].ToString();
                DataSet DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Label_CommitMessage.Text = "内部数据库错误";
                    return;
                }
                if (!Security.MD5.Exam(TextBox_PASSWORD.Text, System.Text.Encoding.ASCII, DS.Tables[0].Rows[0][0].ToString()))
                {
                    Label_CommitMessage.Text = "登录密码错误";
                    return;
                }
                CmdStr = @"UPDATE CHEQUE_INFO SET [PASSWORD]='" + Security.MD5.HexCompute(TextBox_NewPassword.Text, System.Text.Encoding.ASCII) + "' WHERE [CHEQUE_ID] = " + Session["chequeIDToResetPassword"].ToString();
                if (!Logic.Logic.UpdateData(CmdStr))
                {
                    Label_CommitMessage.Text = "内部数据库错误";
                    return;
                }
                Label_CommitMessage.Visible = false;
                Response.Redirect("~/Cheque/ManageCheque.aspx");
            }
            // 其它地方已经验证好，直接写数据库就行了。
        }

//         protected void LoadCheque(object sender, EventArgs e)
//         {
//             if (FileUpload_ChequeFile.HasFile)
//             {
//                 Image_ChequeImage.Visible = true;
//                 CQFileStream = FileUpload_ChequeFile.FileContent;
//                 cf = new Logic.Cheque(CQFileStream);
//                 int ChequeState = cf.Verify();
//                 if (ChequeState == -1)
//                 {
//                     Label_LoadMessage.Text = "支票文件验证失败";
//                     Image_ChequeImage.Visible = false;
//                 }
//                 else if (ChequeState == 1)
//                 {
//                     Label_LoadMessage.Text = "该支票已经使用";
//                 }
//                 else if (ChequeState == 2)
//                 {
//                     Label_LoadMessage.Text = "该支票已经注销";
//                 }
//                 Session["ChequeFileStream"] = CQFileStream;
//                 Image_ChequeImage.ImageUrl = "ChequeFileImg.aspx";
//                 return;
//             }
//             Image_ChequeImage.Visible = false;
//             Label_LoadMessage.Text = "请选择支票文件";
//         }

    }
}
